import Toast from '@vant/weapp/toast/toast';
const https = require('../../http/https')
const baseurl = https.https.baseurl
const museum = require('../../areaList/brance_2/1museum')
const temple = require('../../areaList/brance_2/2temple')
const geology = require('../../areaList/brance_2/3geology')
const revolution = require('../../areaList/brance_2/4revolution')
const areaList = require('../../areaList/master/areaList')
const brance_2 = {
  museum,
  temple,
  geology,
  revolution
}
const app = getApp()

function login() {
  return new Promise((resolve, reject) => {
    wx.login({
      success(res) {
        console.log(res.code);
        resolve(res.code)
      },
      fail(err) {
        console.error(err);
        reject(err)
      }
    })
  })
}

function request_login(url, data) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: url, // 请求的服务器地址
      method: 'POST',
      data: data,
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 设置Content-Type
      },
      success(res) {
        console.log(res);
        resolve(res.data)
      },
      fail(err) {
        const res = {
          code:0
        }
        resolve(res)
      }
    })
  })
}

function selectFavorites() {
  return new Promise((resolve) => {
    const number = app.globalData.number
    const url = baseurl + "/route/user/selectfavorites"
    if (number != 0) {
      wx.request({
        url: url,
        method: 'POST',
        data: {
          number: number
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 设置Content-Type
        },
        success(res) {
          console.log(res)
          if (res.data.code == 1) {
            console.log("查询成功");
            resolve(res.data.result)
          }
        },
        fail(err) {
          resolve(null)
        }
      })
    } else {
      console.log("无number");
    }
  })
}


// pages/my/my.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    isShowUserInformation: false,
    user_photo: "../../images/user/用户头像.jpg",
    user_name: "微信用户",
    faviorites_route: [],
    isShowFavRoute: false,
    faviorites_sights: [],
    isShowFavSight: false
  },

  userInformation() {
    console.log(1);
    this.setData({
      isShowUserInformation: true
    })
  },

  onclose() {
    console.log(1);
    if(app.globalData.number != 0) {
      this.setData({
        isShowUserInformation: false
      })
    } else {
      this.setData({
      isShowUserInformation: false,
      user_photo: "../../images/user/用户头像.jpg",
      user_name: "微信用户",
      })
    }
  },

  onChooseAvatar(event) {
    const user_photo = event.detail.avatarUrl

    this.setData({
      user_photo: user_photo
    })
  },

  findMore(event) {
    console.log(event.currentTarget.dataset.item);
    const item = event.currentTarget.dataset.item
    wx.navigateTo({
      url: "/pages/Pfindmore/pages/findmore/findmore?currentId=" + item.currentId +  "&areaList_currentId=" + item.areaListCurrentId,
    })
  },

  //用户登录
  userlogin() {
    //没连服务器的逻辑
    Toast('登录失败')
    this.setData({
      user_photo: "../../images/user/用户头像.jpg",
      user_name: "微信用户"
    })

    //连接服务器的逻辑
    // const that = this
    // const APP = app
    // login().then(code => {
    //   if (code != Promise && code) {
    //     const url = baseurl + "/route/user/login"
    //     const data = {
    //       rName: this.data.user_name,
    //       code: code
    //     }
    //     request_login(url, data).then(res => {
    //       return new Promise((resolve) => {
    //         console.log(res);
    //         if (res.code == 1) {
    //           Toast.success('登录成功')
    //           that.setData({
    //             isShowUserInformation: false
    //           })
    //           APP.globalData.number = res.result
    //           wx.setStorageSync('number', res.result)
    //           resolve(res.result)
    //           wx.setStorageSync('userName', this.data.user_name)
    //           wx.getFileSystemManager().saveFile({
    //             tempFilePath: this.data.user_photo,
    //             success: function (res) {
    //               const savedFilePath = res.savedFilePath;
    //               console.log("文件保存成功", savedFilePath);
    //               wx.setStorageSync('userPhoto', savedFilePath)
    //             },
    //           });
    //         } else if (res.code == 0) {
    //           Toast('登录失败')
    //           that.setData({
    //             user_photo: "../../images/user/用户头像.jpg",
    //             user_name: "微信用户"
    //           })
    //           resolve(0)
    //         }
    //       })
    //     }).then(res => {
    //       if (res != 0) {
    //         //获取收藏
    //         selectFavorites().then(res => {
    //           if (res != null) {
    //             console.log(res);
    //             let faviorites_route = []
    //             let faviorites_sights = []
    //             let count = 0
    //             res.map(item => {
    //               const object = {
    //                 id: count,
    //                 img: areaList.areaList[item.areaListCurrentId].pointsList[item.currentId].bgi_url,
    //                 name: item.name,
    //                 remark: item.remark,
    //                 areaListCurrentId: item.areaListCurrentId,
    //                 currentId: item.currentId
    //               }
    //               //是路径
    //               if (item.currentId == 0) {
    //                 faviorites_route.push(object)
    //               } else if (item.currentId != 0) {
    //                 faviorites_sights.push(object)
    //               }
    //               count++
    //             })

    //             this.setData({
    //               faviorites_route: faviorites_route,
    //               faviorites_sights: faviorites_sights
    //             })

    //             wx.setStorageSync('faviorites_route', JSON.stringify(faviorites_route))
    //             wx.setStorageSync('faviorites_sights', JSON.stringify(faviorites_sights))
    //           } else {
    //             console.log("获取收藏失败");
    //           }
    //         })
    //       }
    //     })
    //   }
    // })
  },

  onClose() {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //获取头像和名字
    const user_name = wx.getStorageSync('userName')
    const user_photo = wx.getStorageSync('userPhoto')
    if (user_name && user_photo) {
      this.setData({
        user_name: user_name,
        user_photo: user_photo
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //获取收藏路径
    const route = wx.getStorageSync('faviorites_route')
    const sights = wx.getStorageSync('faviorites_sights')
    if (route != "" && sights != "") {
      const faviorites_route = JSON.parse(route)
      const faviorites_sights = JSON.parse(sights)
      //本地有收藏数据
      this.setData({
        faviorites_route: faviorites_route,
        faviorites_sights: faviorites_sights
      })
    } else if (app.globalData.number != 0) {
      //本地没有收藏数据，但有number可以请求
      selectFavorites().then(res => {
        if (res != null) {
          console.log(res);
          let faviorites_route = []
          let faviorites_sights = []
          let count = 0
          res.map(item => {
            const object = {
              id: count,
              img: areaList.areaList[item.areaListCurrentId].pointsList[item.currentId].bgi_url,
              name: item.name,
              remark: item.remark,
              areaListCurrentId: item.areaListCurrentId,
              currentId: item.currentId
            }
            //是路径
            if (item.currentId == 0) {
              faviorites_route.push(object)
            } else if (item.currentId != 0) {
              faviorites_sights.push(object)
            }
            count++
          })

          this.setData({
            faviorites_route: faviorites_route,
            faviorites_sights: faviorites_sights
          })

          wx.setStorageSync('faviorites_route', JSON.stringify(faviorites_route))
          wx.setStorageSync('faviorites_sights', JSON.stringify(faviorites_sights))
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})